using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GamePlay;

namespace MainGame.Menu
{

  public interface INewGameMenu
  {
    event MenuEvent OnBackClicked;
    event MenuEvent OnPlayClicked;
  }

  public struct gameParameter
  {
    public String name;
    public bool isH;
    public bool isActive;
  }  

  public class NewGameMenu : BaseMenu, INewGameMenu
  {
    private Button p1button, p2button, p3button, p4button, BackBtn, StartBtn, StartBtn2;
    private Button p1HTypebutton, p2HTypebutton, p3HTypebutton, p4HTypebutton;
    private Button p2AiTypebutton, p3AiTypebutton, p4AiTypebutton;
    private Button p3Activebutton, p4Activebutton;

    //public gameParameter[] gameParameters = new gameParameter[4];

    private bool p3Active = false, p4Active = false;

    private Texture2D box, x, v;

    SpriteBatch spriteBatch;

    private Color p1Color = Color.DeepSkyBlue, p2Color = Color.LawnGreen, p3Color = Color.Tomato, p4Color = Color.Yellow;
    private Color noActiveColor = new Color(128, 128, 128, 150);

    public NewGameMenu(Game game, MenuDepths depth)
      : base(game, depth)
    {
      spriteBatch = Game.GetContext().SpriteBatch;
      background = game.Content.Load<Texture2D>(@"Images//Menu//newgamebgnd");
      bounds = new Rectangle(0, 0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height);
      game.GetContext().gameParameters = new gameParameter[4];

      game.Services.AddService(typeof(INewGameMenu), this);
    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(INewGameMenu));
      base.Dispose(disposing);
    }

    Random rnd = new Random();

    public override void Initialize()
    {
      //Game.GetContext().gameParameters[0].name = Locale.Strings.Player + "1";
      //Game.GetContext().gameParameters[1].name = Locale.Strings.Player + "2";
      //Game.GetContext().gameParameters[2].name = Locale.Strings.Player + "3";
      //Game.GetContext().gameParameters[3].name = Locale.Strings.Player + "4";     

      List<int> n= new List<int>();

      for (int i = 0; i < 4; i++)
      {
        int r = rnd.Next(0, 10);
        if (!n.Contains(r))
          n.Add(r);
        else i--;

        r = n.Last();

        String name = Locale.Strings.Player + "i";

        if (r == 0) name = Locale.Strings.AiPlayerName00;
        if (r == 1) name = Locale.Strings.AiPlayerName01;
        if (r == 2) name = Locale.Strings.AiPlayerName02;
        if (r == 3) name = Locale.Strings.AiPlayerName03;
        if (r == 4) name = Locale.Strings.AiPlayerName04;
        if (r == 5) name = Locale.Strings.AiPlayerName05;
        if (r == 6) name = Locale.Strings.AiPlayerName06; 
        if (r == 7) name = Locale.Strings.AiPlayerName07;
        if (r == 8) name = Locale.Strings.AiPlayerName08;
        if (r == 9) name = Locale.Strings.AiPlayerName09;
        if (r == 0) name = Locale.Strings.AiPlayerName00;

        Game.GetContext().gameParameters[i].name = name;
      }

      Game.GetContext().gameParameters[0].name = Game.GetContext().DefaultName;

      Game.GetContext().gameParameters[0].isH = true;
      Game.GetContext().gameParameters[1].isH = false;
      Game.GetContext().gameParameters[2].isH = false;
      Game.GetContext().gameParameters[3].isH = false;

      Game.GetContext().gameParameters[0].isActive = true;
      Game.GetContext().gameParameters[1].isActive = true;
      Game.GetContext().gameParameters[2].isActive = false;
      Game.GetContext().gameParameters[3].isActive = false;

      box = Game.Content.Load<Texture2D>(@"Images\\Menu\\CheckBox");
      x = Game.Content.Load<Texture2D>(@"Images\\Menu\\x");
      v = Game.Content.Load<Texture2D>(@"Images\\Menu\\v");

      // Back button
      BackBtn = new Button(Game, @"Images//Menu//button2", new Rectangle((int)bounds.X + 620, 370, 140, 55), Color.Blue, "small_font", Locale.Strings.Back, Color.White);
      AddButton(BackBtn, () =>
      {
        BackBtn.touched = true;

        //if (OnBackClicked != null)
        //  OnBackClicked();  
      
      });

      // Start button
      StartBtn = new Button(Game, @"Images//Menu//button2", new Rectangle((int)bounds.X + 620, 50, 140, 55), Color.Green, "small_font", Locale.Strings.Tournament, Color.White);
      AddButton(StartBtn, () =>
      {
        StartBtn.touched = true;

        Game.GetContext().QuickGame = false;
        Game.GetContext().GameFinished = false;

        Game.GetContext().PlayerTurn = 0;

        //if (OnPlayClicked != null)
        //  OnPlayClicked();

      });

      // Quick Game
      StartBtn2 = new Button(Game, @"Images//Menu//button2", new Rectangle((int)bounds.X + 610, 170, 160, 55), new Color(34, 200, 34), "small_font", Locale.Strings.QuickGame, Color.White);
      AddButton(StartBtn2, () =>
      {
        StartBtn2.touched = true;

        Game.GetContext().QuickGame = true;
        Game.GetContext().GameFinished = false;
      });

      // Player1
      // Name
      p1button = new Button(Game, @"Images//Menu//button2", new Rectangle((int)bounds.X + 150, 50, 220, 70), p1Color, "small_font", Game.GetContext().gameParameters[0].name, Color.Black);
      AddButton(p1button, () =>
      {
        p1button.touched = true;
        StateManagement.StateManager.keyboardMenu.Visible = true;
        StateManagement.StateManager.keyboardMenu.Enabled = true;
        this.Enabled = false;

        var keyboard = Game.Services.GetService(typeof(IKeyboardMenu)) as IKeyboardMenu;
        keyboard.OnSaveClicked += () => 
        {
          Game.GetContext().gameParameters[0].name = StateManagement.StateManager.keyboardMenu.word;
          Game.GetContext().DefaultName = Game.GetContext().gameParameters[0].name;
          p1button.message = Game.GetContext().gameParameters[0].name;
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();
        };

        keyboard.OnExitClicked += () => 
        {
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();
        };

      });

      p1HTypebutton = new Button(Game, @"Images//Menu//Player1", new Rectangle((int)bounds.X + 400, 60, 55, 55), p1Color, "small_font", "", Color.White);
      AddButton(p1HTypebutton, () =>
      {});


      // Player 2
      p2button = new Button(Game, @"Images//Menu//button2", new Rectangle((int)bounds.X + 150, 150, 220, 70), p2Color, "small_font", Game.GetContext().gameParameters[1].name, Color.Black);
      AddButton(p2button, () =>
      {
        p2button.touched = true;
        StateManagement.StateManager.keyboardMenu.Visible = true;
        StateManagement.StateManager.keyboardMenu.Enabled = true;
        this.Enabled = false;

        var keyboard = Game.Services.GetService(typeof(IKeyboardMenu)) as IKeyboardMenu;
        keyboard.OnSaveClicked += () => 
        {
          Game.GetContext().gameParameters[1].name = StateManagement.StateManager.keyboardMenu.word;
          p2button.message = Game.GetContext().gameParameters[1].name;
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();

        };

        keyboard.OnExitClicked += () =>
        {
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();
        };

      });

      p2HTypebutton = new Button(Game, @"Images//Menu//Player1", new Rectangle((int)bounds.X + 400, 160, 55, 55), p2Color, "small_font", "", Color.White);
      AddButton(p2HTypebutton, () =>
      {
        Game.GetContext().gameParameters[1].isH = true;
      });

      p2AiTypebutton = new Button(Game, @"Images//Menu//PlayerAi", new Rectangle((int)bounds.X + 500, 160, 55, 55), p2Color, "small_font", "", Color.White);
      AddButton(p2AiTypebutton, () =>
      {
        Game.GetContext().gameParameters[1].isH = false;
      });

      // Player3

      p3button = new Button(Game, @"Images//Menu//button2", new Rectangle((int)bounds.X + 150, 250, 220, 70), noActiveColor, "small_font", Game.GetContext().gameParameters[2].name, Color.Black);
      AddButton(p3button, () =>
      {
        if (!p3Active) return;

        p3button.touched = true;
        StateManagement.StateManager.keyboardMenu.Visible = true;
        StateManagement.StateManager.keyboardMenu.Enabled = true;
        this.Enabled = false;

        var keyboard = Game.Services.GetService(typeof(IKeyboardMenu)) as IKeyboardMenu;
        keyboard.OnSaveClicked += () =>
        {
          Game.GetContext().gameParameters[2].name = StateManagement.StateManager.keyboardMenu.word;
          p3button.message = Game.GetContext().gameParameters[2].name;
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();
        };

        keyboard.OnExitClicked += () =>
        {
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();
        };

      });

      p3HTypebutton = new Button(Game, @"Images//Menu//Player1", new Rectangle((int)bounds.X + 400, 260, 55, 55), noActiveColor, "small_font", "", Color.White);
      AddButton(p3HTypebutton, () =>
      {
        if (!p3Active) return;

        Game.GetContext().gameParameters[2].isH = true;
      });

      p3AiTypebutton = new Button(Game, @"Images//Menu//PlayerAi", new Rectangle((int)bounds.X + 500, 260, 55, 55), noActiveColor, "small_font", "", Color.White);
      AddButton(p3AiTypebutton, () =>
      {
        if (!p3Active) return;

        Game.GetContext().gameParameters[2].isH = false;
      });

      p3Activebutton = new Button(Game, @"Images//Menu//x", new Rectangle((int)bounds.X + 50, 260, 55, 55), Color.White, "small_font", "", Color.White);
      AddButton(p3Activebutton, () =>
      {
        p3Active = !p3Active;

        if (p3Active)
        {
          Game.GetContext().gameParameters[2].isActive = true;

          p3button.color = p3Color;
          p3HTypebutton.color = p3Color;
          p3AiTypebutton.color = p3Color;

          p3Activebutton.texture = v;
        }

        if (!p3Active)
        {
          Game.GetContext().gameParameters[2].isActive = false;

          p3button.color = noActiveColor;
          p3HTypebutton.color = noActiveColor;
          p3AiTypebutton.color = noActiveColor;

          p3Activebutton.texture = x;
        }
      });

      // Player 4

      p4button = new Button(Game, @"Images//Menu//button2", new Rectangle((int)bounds.X + 150, 350, 220, 70), noActiveColor, "small_font", Game.GetContext().gameParameters[3].name, Color.Black);
      AddButton(p4button, () =>
      {
        if (!p4Active) return;

        p4button.touched = true;
        StateManagement.StateManager.keyboardMenu.Visible = true;
        StateManagement.StateManager.keyboardMenu.Enabled = true;
        this.Enabled = false;

        var keyboard = Game.Services.GetService(typeof(IKeyboardMenu)) as IKeyboardMenu;

        keyboard.OnSaveClicked += () =>
        {
          Game.GetContext().gameParameters[3].name = StateManagement.StateManager.keyboardMenu.word;
          p4button.message = Game.GetContext().gameParameters[3].name;
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();
        };

        keyboard.OnExitClicked += () =>
        {
          StateManagement.StateManager.keyboardMenu.Visible = false;
          StateManagement.StateManager.keyboardMenu.Enabled = false;
          this.Enabled = true;

          ResetKeyboard();
        };
      });

      p4HTypebutton = new Button(Game, @"Images//Menu//Player1", new Rectangle((int)bounds.X + 400, 360, 55, 55),noActiveColor, "small_font", "", Color.White);
      AddButton(p4HTypebutton, () =>
      {
        if (!p4Active) return;

        Game.GetContext().gameParameters[3].isH = true;
      });

      p4AiTypebutton = new Button(Game, @"Images//Menu//PlayerAi", new Rectangle((int)bounds.X + 500, 360, 55, 55), noActiveColor, "small_font", "", Color.White);
      AddButton(p4AiTypebutton, () =>
      {
        if (!p4Active) return;

        Game.GetContext().gameParameters[3].isH = false;
      });

      p4Activebutton = new Button(Game, @"Images//Menu//x", new Rectangle((int)bounds.X + 50, 360, 55, 55), Color.White, "small_font", "", Color.White);
      AddButton(p4Activebutton, () =>
      {
        p4Active = !p4Active;

        if (p4Active)
        {
          Game.GetContext().gameParameters[3].isActive = true;

          p4button.color = p4Color;
          p4HTypebutton.color = p4Color;
          p4AiTypebutton.color = p4Color;

          p4Activebutton.texture = v;
        }

        if (!p4Active)
        {
          Game.GetContext().gameParameters[3].isActive = false;

          p4button.color = noActiveColor;
          p4HTypebutton.color = noActiveColor;
          p4AiTypebutton.color = noActiveColor;

          p4Activebutton.texture = x;
        }
      });

      base.Initialize();
    }

    private void ResetKeyboard()
    {
      Game.Services.RemoveService(typeof(IKeyboardMenu));

      StateManagement.StateManager.keyboardMenu = new KeyboardMenu(Game, MenuDepths.ForeGround);
      Game.Components.Add(StateManagement.StateManager.keyboardMenu);

      StateManagement.StateManager.keyboardMenu.Animated = true;
      StateManagement.StateManager.keyboardMenu.Enabled = false;
      StateManagement.StateManager.keyboardMenu.Visible = false;
    }

    int counter = 4;
    public override void Update(GameTime gameTime)
    {
      // Input: back button pressed
      if (((Game1)Game).IsBackButtonPressed())
      {
        if (OnBackClicked != null)
          OnBackClicked();  
        
      }

      if (StartBtn.touched)
      {
        counter--;
        if (counter < 0)
        {
          StartBtn.touched = false;
          if ((OnPlayClicked != null))
            OnPlayClicked();
          counter = 4;
        }
      }

      if (StartBtn2.touched)
      {
        counter--;
        if (counter < 0)
        {
          StartBtn2.touched = false;
          if ((OnPlayClicked != null))
            OnPlayClicked();
          counter = 4;
        }
      }

      if (BackBtn.touched)
      {
        counter--;
        if (counter < 0)
        {
          BackBtn.touched = false;
          if (OnBackClicked != null)
            OnBackClicked();
          counter = 4;
        }
      }

      if (p1button.touched)
      {
        counter--;
        if (counter < 0)
        {
          p1button.touched = false;
          counter = 4;
        }
      }

      if (p2button.touched)
      {
        counter--;
        if (counter < 0)
        {
          p2button.touched = false;
          counter = 4;
        }
      }

      if (p3button.touched)
      {
        counter--;
        if (counter < 0)
        {
          p3button.touched = false;
          counter = 4;
        }
      }

      if (p4button.touched)
      {
        counter--;
        if (counter < 0)
        {
          p4button.touched = false;
          counter = 4;
        }
      }

      base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
      base.Draw(gameTime);

      spriteBatch.Begin();

      if (Game.GetContext().gameParameters[1].isH) spriteBatch.Draw(box, new Rectangle(400, 160, 55, 55), Color.White);
      else spriteBatch.Draw(box, new Rectangle(500, 160, 55, 55), Color.White);

      if (Game.GetContext().gameParameters[2].isH) spriteBatch.Draw(box, new Rectangle(400, 260, 55, 55), Color.White);
      else spriteBatch.Draw(box, new Rectangle(500, 260, 55,55), Color.White);

      if (Game.GetContext().gameParameters[3].isH) spriteBatch.Draw(box, new Rectangle(400, 360, 55, 55), Color.White);
      else spriteBatch.Draw(box, new Rectangle(500, 360, 55, 55), Color.White);
     
      spriteBatch.End();

    }

    public event MenuEvent OnBackClicked;
    public event MenuEvent OnPlayClicked;
  }
}
